/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia.JPAControllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Logica.Core.Depositos.Lote;
import Logica.Core.Personal.Insumo;
import Logica.Core.Producto.YerbaVerde;
import Persistencia.JPAControllers.exceptions.NonexistentEntityException;
import Persistencia.JPAControllers.exceptions.PreexistingEntityException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author DANIEL
 */
public class YerbaVerdeJpaController implements Serializable {

    public YerbaVerdeJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public YerbaVerdeJpaController() {
        emf = Persistence.createEntityManagerFactory("SecaderoPU");
    }

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(YerbaVerde yerbaVerde) throws PreexistingEntityException, Exception {
        if (yerbaVerde.getMisInsumos() == null) {
            yerbaVerde.setMisInsumos(new ArrayList<Insumo>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Lote miLote = yerbaVerde.getMiLote();
            if (miLote != null) {
                miLote = em.getReference(miLote.getClass(), miLote.getNumero());
                yerbaVerde.setMiLote(miLote);
            }
            List<Insumo> attachedMisInsumos = new ArrayList<Insumo>();
            for (Insumo misInsumosInsumoToAttach : yerbaVerde.getMisInsumos()) {
                misInsumosInsumoToAttach = em.getReference(misInsumosInsumoToAttach.getClass(), misInsumosInsumoToAttach.getId());
                attachedMisInsumos.add(misInsumosInsumoToAttach);
            }
            yerbaVerde.setMisInsumos(attachedMisInsumos);
            em.persist(yerbaVerde);
            if (miLote != null) {
                Logica.Core.Producto.Producto oldMiProductoOfMiLote = miLote.getMiProducto();
                if (oldMiProductoOfMiLote != null) {
                    oldMiProductoOfMiLote.setMiLote(null);
                    oldMiProductoOfMiLote = em.merge(oldMiProductoOfMiLote);
                }
                miLote.setMiProducto(yerbaVerde);
                miLote = em.merge(miLote);
            }
            for (Insumo misInsumosInsumo : yerbaVerde.getMisInsumos()) {
                misInsumosInsumo.getMisProductos().add(yerbaVerde);
                misInsumosInsumo = em.merge(misInsumosInsumo);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findYerbaVerde(yerbaVerde.getId()) != null) {
                throw new PreexistingEntityException("YerbaVerde " + yerbaVerde + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(YerbaVerde yerbaVerde) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            YerbaVerde persistentYerbaVerde = em.find(YerbaVerde.class, yerbaVerde.getId());
            Lote miLoteOld = persistentYerbaVerde.getMiLote();
            Lote miLoteNew = yerbaVerde.getMiLote();
            List<Insumo> misInsumosOld = persistentYerbaVerde.getMisInsumos();
            List<Insumo> misInsumosNew = yerbaVerde.getMisInsumos();
            if (miLoteNew != null) {
                miLoteNew = em.getReference(miLoteNew.getClass(), miLoteNew.getNumero());
                yerbaVerde.setMiLote(miLoteNew);
            }
            List<Insumo> attachedMisInsumosNew = new ArrayList<Insumo>();
            for (Insumo misInsumosNewInsumoToAttach : misInsumosNew) {
                misInsumosNewInsumoToAttach = em.getReference(misInsumosNewInsumoToAttach.getClass(), misInsumosNewInsumoToAttach.getId());
                attachedMisInsumosNew.add(misInsumosNewInsumoToAttach);
            }
            misInsumosNew = attachedMisInsumosNew;
            yerbaVerde.setMisInsumos(misInsumosNew);
            yerbaVerde = em.merge(yerbaVerde);
            if (miLoteOld != null && !miLoteOld.equals(miLoteNew)) {
                miLoteOld.setMiProducto(null);
                miLoteOld = em.merge(miLoteOld);
            }
            if (miLoteNew != null && !miLoteNew.equals(miLoteOld)) {
                Logica.Core.Producto.Producto oldMiProductoOfMiLote = miLoteNew.getMiProducto();
                if (oldMiProductoOfMiLote != null) {
                    oldMiProductoOfMiLote.setMiLote(null);
                    oldMiProductoOfMiLote = em.merge(oldMiProductoOfMiLote);
                }
                miLoteNew.setMiProducto(yerbaVerde);
                miLoteNew = em.merge(miLoteNew);
            }
            for (Insumo misInsumosOldInsumo : misInsumosOld) {
                if (!misInsumosNew.contains(misInsumosOldInsumo)) {
                    misInsumosOldInsumo.getMisProductos().remove(yerbaVerde);
                    misInsumosOldInsumo = em.merge(misInsumosOldInsumo);
                }
            }
            for (Insumo misInsumosNewInsumo : misInsumosNew) {
                if (!misInsumosOld.contains(misInsumosNewInsumo)) {
                    misInsumosNewInsumo.getMisProductos().add(yerbaVerde);
                    misInsumosNewInsumo = em.merge(misInsumosNewInsumo);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                int id = yerbaVerde.getId();
                if (findYerbaVerde(id) == null) {
                    throw new NonexistentEntityException("The yerbaVerde with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(int id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            YerbaVerde yerbaVerde;
            try {
                yerbaVerde = em.getReference(YerbaVerde.class, id);
                yerbaVerde.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The yerbaVerde with id " + id + " no longer exists.", enfe);
            }
            Lote miLote = yerbaVerde.getMiLote();
            if (miLote != null) {
                miLote.setMiProducto(null);
                miLote = em.merge(miLote);
            }
            List<Insumo> misInsumos = yerbaVerde.getMisInsumos();
            for (Insumo misInsumosInsumo : misInsumos) {
                misInsumosInsumo.getMisProductos().remove(yerbaVerde);
                misInsumosInsumo = em.merge(misInsumosInsumo);
            }
            em.remove(yerbaVerde);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<YerbaVerde> findYerbaVerdeEntities() {
        return findYerbaVerdeEntities(true, -1, -1);
    }

    public List<YerbaVerde> findYerbaVerdeEntities(int maxResults, int firstResult) {
        return findYerbaVerdeEntities(false, maxResults, firstResult);
    }

    private List<YerbaVerde> findYerbaVerdeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from YerbaVerde as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public YerbaVerde findYerbaVerde(int id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(YerbaVerde.class, id);
        } finally {
            em.close();
        }
    }

    public int getYerbaVerdeCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from YerbaVerde as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
